/**
 * 初始化质控规则详情对话框
 */
var SdcQcRuleInfoDlg = {
    qcRuleData:  {},
    validateFields: {
    	expCode: {
            validators: {
                notEmpty: {
                    message: '规则代码不能为空'
                }
            }
        },
    	ruleDesc: {
            validators: {
                notEmpty: {
                    message: '规则描述不能为空'
                }
            }
        },
    	ruleExpression: {
            validators: {
                notEmpty: {
                    message: '表达式不能为空'
                }
            }
        },
    	ruleParm: {
            validators: {
                notEmpty: {
                    message: '参数不能为空'
                }
            }
        }
    }
};

SdcQcRuleInfoDlg.search = function () {
    SdcQcRuleInfoDlg.initSourceForm();
};

SdcQcRuleInfoDlg.clearData = function() {
    this.qcRuleData = {};
}

SdcQcRuleInfoDlg.set = function(key, value) {
    this.qcRuleData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

SdcQcRuleInfoDlg.get = function(key) {
    return $("#" + key).val();
}

SdcQcRuleInfoDlg.sourceChange1 = function(source) {
    var sourceId = source.value;
    $('#dataSourceItem1').combobox("clear");
    //根据父级机构 查询子机构
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/getSourceFieldAndStandDic", function(res){
        var fieldMap = $.parseJSON(res.fieldMap);
        $('#dataSourceItem1').combobox({
            data: fieldMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"sourceId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}

SdcQcRuleInfoDlg.sourceChange2 = function(source) {
    var sourceId = source.value;
    $('#dataSourceItem2').combobox("clear");
    //根据父级机构 查询子机构
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/getSourceFieldAndStandDic", function(res){
        var fieldMap = $.parseJSON(res.fieldMap);
        $('#dataSourceItem2').combobox({
            data: fieldMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"sourceId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}

SdcQcRuleInfoDlg.collectData = function () {
    this.set('dataSource1',$('#dataSource1').combobox('getValue')).set('dataSourceItem1',$('#dataSourceItem1').combobox('getValue'))
        .set('dataSource2',$('#dataSource2').combobox('getValue')).set('dataSourceItem2',$('#dataSourceItem2').combobox('getValue'))
        .set('param1',encodeURIComponent($('#param1').val())).set('ruleMemo',$('#ruleMemo').val())
        .set('param2',encodeURIComponent($('#param2').val())).set('operator1',encodeURIComponent($('#operator1').val()))
        .set('operator2',encodeURIComponent($('#operator2').val()))
        .set('ruleDesc',$('#ruleDesc').val()).set('expCode',$("#expCode").val()).set('id',$("#id").val());
};


/**
 * 关闭此对话框
 */
SdcQcRuleInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭   
}

/**
 * 验证数据是否为空
 */
SdcQcRuleInfoDlg.validate = function () {
    $('#sdcQcRuleInfoForm').data("bootstrapValidator").resetForm();
    $('#sdcQcRuleInfoForm').bootstrapValidator('validate');
    return $("#sdcQcRuleInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
SdcQcRuleInfoDlg.addSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //this.collectData();
    //提交信息
    /*Sys.ajaxForm('sdcQcRuleInfoForm',Sys.backendPath + "/sdc/sdcqcconfig/addRule",function(res) {
        Sys.success(res.message);
    	//window.parent.SdcQcRule.table.refresh();
    	window.parent.obj.qCChange();
        SdcQcRuleInfoDlg.close();
    });*/
    var queryData = {};
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcqcconfig/addRule", function (data) {
        //SdsDataConfig.initSourceForm();
        Sys.success("修改成功!");
        window.parent.obj.qCChange();
        SdcQcRuleInfoDlg.close();
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    //ajax.set(this.qcRuleData);
    queryData['id'] = $("#id").val();
    queryData['expCode'] = Common_GetValue('expCode');
    queryData['ruleDesc'] = Common_GetValue('ruleDesc');
    queryData['ruleMemo'] = Common_GetValue('ruleMemo');
    queryData['dataSource1'] = Common_GetValue('dataSource1');
    queryData['dataSource2'] = Common_GetValue('dataSource2');
    queryData['dataSourceItem1'] = Common_GetValue('dataSourceItem1');
    queryData['dataSourceItem2'] = Common_GetValue('dataSourceItem2');
    queryData['param1'] = encodeURIComponent(Common_GetValue('param1'));
    queryData['param2'] = encodeURIComponent(Common_GetValue('param2'));
    queryData['operator1'] = encodeURIComponent(Common_GetValue('operator1'));
    queryData['operator2'] = encodeURIComponent(Common_GetValue('operator2'));
    queryData['ruleType'] = Common_GetValue('ruleType');
    queryData['defaultValue'] = Common_GetValue('defaultValue');
    ajax.setData(queryData);
    ajax.start();
}

/**
 * 提交修改
 */
SdcQcRuleInfoDlg.editSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    Sys.ajaxForm('sdcQcRuleInfoForm',Sys.backendPath + "/sdc/sdcqcconfig/updateRule",function(res) {
        Sys.success(res.message);
        window.parent.obj.qCChange();
        SdcQcRuleInfoDlg.close();
    });
}

function Common_GetValue() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

SdcQcRuleInfoDlg.initSourceForm = function (){
    var params = {};
    params.id = $("#id").val();
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcqcconfig/config", function(data){
        $("#id").val(data.id);
        $("#dataSource1").combobox("select", data.dataSource1)
        $("#dataSource2").combobox("select", data.dataSource2);
        $("#dataSourceItem1").combobox("select", data.dataSourceItem1);
        $("#dataSourceItem2").combobox("select", data.dataSourceItem2);
        $("#param1").val(data.param1);
        $("#param2").val(data.param2);
        $("#operator1").val(data.operator1);
        $("#operator2").val(data.operator2);

        $("#expCode").val(data.expCode);
        $("#ruleDesc").val(data.ruleDesc);
        $("#ruleMemo").val(data.ruleMemo);
        $("#ruleType").combobox("select", data.ruleType);
        $("#defaultValue").combobox("select", data.defaultValue);
    },function (data){
    });
    ajax.setData(params);
    ajax.start();

    /*if ($("#sdcQcRule").val() != null){
        var data = $.parseJSON($("#sdcQcRule").val());
        $("#id").val(data.id);
        $("#dataSource1").combobox("select", data.dataSource1)
        $("#dataSource2").combobox("select", data.dataSource2);
        $("#dataSourceItem1").combobox("select", data.dataSourceItem1);
        $("#dataSourceItem2").combobox("select", data.dataSourceItem2);
        $("#param1").val(data.param1);
        $("#param2").val(data.param2);
        $("#operator1").val(data.operator1);
        $("#operator2").val(data.operator2);

        $("#expCode").val(data.expCode);
        $("#ruleDesc").val(data.ruleDesc);
        $("#ruleMemo").val(data.ruleMemo);
    }*/
}

$(function() {
	Sys.initValidator("sdcQcRuleInfoForm", SdcQcRuleInfoDlg.validateFields);

    $HUI.combobox('#ruleType',
        {
            data:[
                {'ruleType':'Day','Desc':"天数"},
                {'ruleType':'Hour','Desc':"小时"},
                {'ruleType':'Minute','Desc':"分钟"},
                {'ruleType':'String','Desc':"字符串"}
            ],
            valueField:'ruleType',
            textField:'Desc',
        })

    $HUI.combobox('#defaultValue',
        {
            data:[
                {'value':'0','Desc':"false"},
                {'value':'1','Desc':"true"}
            ],
            valueField:'value',
            textField:'Desc',
        })

    /*$HUI.combobox('#dataSource1',
        {
            data:$.parseJSON($("#dataSourceMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6
        })
    $HUI.combobox('#dataSource2',
        {
            data:$.parseJSON($("#dataSourceMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6
        })*/

    SdcQcRuleInfoDlg.initSourceForm();
});
